package util;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {

	private static final String delim = ";";
	
	public static HashMap<String, Triplet<Candle, Candle, Candle>> matchCandleTriplets(HashMap<String, Candle> candles_2, HashMap<String, Candle> candles_1, HashMap<String, Candle> candles_0) {
		HashMap<String, Triplet<Candle, Candle, Candle>> candleTriplets = new HashMap<String, Triplet<Candle, Candle, Candle>>();
		Iterator<String> iter_2 = candles_2.keySet().iterator();
		while (iter_2.hasNext()) {
			String stockName = iter_2.next();
			Candle candle_2 = candles_2.get(stockName);
			Candle candle_1 = candles_1.get(stockName);
			Candle candle_0 = candles_0.get(stockName);
			
			if (candle_2 != null && candle_1 != null && candle_0 != null) {
				candleTriplets.put(stockName, new Triplet<Candle, Candle, Candle>(candle_2, candle_1, candle_0));
			}
		}
		
		return candleTriplets;
	}
	
	public static HashMap<Candle, Candle> matchCandlePairs(HashMap<String, Candle> candlesYtd, HashMap<String, Candle> candlesToday) {
		HashMap<Candle, Candle> candlePairs = new HashMap<Candle, Candle>();
		Iterator<String> iterYtd = candlesYtd.keySet().iterator();
		while (iterYtd.hasNext()) {
			String stockName = iterYtd.next();
			Candle candleYtd = candlesYtd.get(stockName);
			Candle candleToday = candlesToday.get(stockName);
			
			if (candleYtd != null && candleToday != null) {
				candlePairs.put(candleYtd, candleToday);
			}
		}
		
		return candlePairs;
	}
	
	public static void readFile(String filename, HashMap<String, Candle> candles) {
		try {
			FileInputStream fstream = new FileInputStream(filename);
			   DataInputStream in = new DataInputStream(fstream);
			   BufferedReader br = new BufferedReader(new InputStreamReader(in));
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
			   String line;
				int numSuspension = 0;
				int numStocks = 0;
			while ((line = br.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, delim);
				Date date = df.parse(st.nextToken());
				String stockName = st.nextToken().trim();
				st.nextToken();
				st.nextToken();
				float priceHigh = Float.parseFloat(st.nextToken());
				float priceLow = Float.parseFloat(st.nextToken());
				float priceClosed = Float.parseFloat(st.nextToken());
				float priceChanged = Float.parseFloat(st.nextToken());
				int volume = Integer.parseInt(st.nextToken());
				st.nextToken();
				st.nextToken();
				st.nextToken();
				float priceOpened = Float.parseFloat(st.nextToken());
				Candle candle = new Candle(stockName, date, volume, priceHigh, priceLow, priceOpened, priceClosed);
				Pattern pattern = Pattern.compile("\\d+");
				Matcher matcher = pattern.matcher(candle.getStockName());
				if (matcher.find()) {
				} else if (stockName.contains("ETF")) {
				} else {
					candles.put(stockName, candle);
					numStocks++;
					if (line.contains("SUSP")) {
						//System.out.println("suspended: " + stockName);
						numSuspension++;
					}
				}
			}
			//System.out.println("#SUSP/#stocks: " + numSuspension + "/" + numStocks);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void readFile(String filename, ArrayList<Candle> candles) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename),"UTF-8"));
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
			String line;
			int numSuspension = 0;
			int numStocks = 0;
			while ((line = br.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, delim);
				Date date = df.parse(st.nextToken());
				String stockName = st.nextToken().trim();
				st.nextToken();
				st.nextToken();
				float priceHigh = Float.parseFloat(st.nextToken());
				float priceLow = Float.parseFloat(st.nextToken());
				float priceClosed = Float.parseFloat(st.nextToken());
				float priceChanged = Float.parseFloat(st.nextToken());
				int volume = Integer.parseInt(st.nextToken());
				st.nextToken();
				st.nextToken();
				st.nextToken();
				float priceOpened = Float.parseFloat(st.nextToken());
				Candle candle = new Candle(stockName, date, volume, priceHigh, priceLow, priceOpened, priceClosed);
				Pattern pattern = Pattern.compile("\\d+");
				Matcher matcher = pattern.matcher(candle.getStockName());
				if (matcher.find()) {
				} else if (stockName.contains("ETF")) {
				} else {
					candles.add(candle);
					numStocks++;
					if (line.contains("SUSP")) numSuspension++;
				}
			}
			//System.out.println("#SUSP/#stocks: " + numSuspension + "/" + numStocks);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	 public static double getEMA(int numberOfDays, double[] prices, int startindex) {
	     double numerator = 0;
	     double denominator = 0;
	      
	     double exponent = 2.0 / (numberOfDays + 1);
	     double exp = 1.0 - exponent;

	     double average = 0.0;
	     for (int i = 0; i <= startindex; i++)
	     {
	         average += prices[i];
	     }
	     average = average / (startindex + 1);
	      
	     //exponential moving average
	     int i = 0;
	     for (i = startindex + numberOfDays - 1; i > startindex; i--)
	     {
	         numerator += prices[i]* Math.pow(exp, startindex + numberOfDays - i - 1);
	         denominator += Math.pow(exp, startindex + numberOfDays - i - 1);
	     }

	     numerator += average * Math.pow(exp, startindex + numberOfDays);
	     denominator += Math.pow(exp, startindex + numberOfDays);
	     return numerator / denominator;	 
	 }
	 
	 public static boolean isReitOrTrust(String stockName) {
		 if (stockName.equals("AIMS AMP CAP INDUSTRIAL REIT")) return true;
		 if (stockName.equals("ASCENDAS HOSPITALITY TRUST")) return true;
		 if (stockName.equals("ASCENDAS INDIA TRUST")) return true;
		 if (stockName.equals("ASCENDAS REAL ESTATE INV TRUST")) return true;
		 if (stockName.equals("ASCOTT RESIDENCE TRUST")) return true;
		 if (stockName.equals("CACHE LOGISTICS TRUST")) return true;
		 if (stockName.equals("CAMBRIDGE INDUSTRIAL TRUST")) return true;
		 if (stockName.equals("CDL HOSPITALITY TRUSTS")) return true;
		 if (stockName.equals("CAPITAMALL TRUST")) return true;
		 if (stockName.equals("CAPITARETAIL CHINA TRUST")) return true;
		 if (stockName.equals("CITYSPRING INFRASTRUCT TRUST")) return true;
		 if (stockName.equals("FIRST REAL ESTATE INV TRUST")) return true;
		 if (stockName.equals("FORTUNE REAL ESTATE INV TRUST")) return true;
		 if (stockName.equals("FRASERS CENTREPOINT TRUST")) return true;
		 if (stockName.equals("FRASERS COMMERCIAL TRUST")) return true;
		 if (stockName.equals("HUTCHISON PORT HLDGS TRUST S$")) return true;
		 if (stockName.equals("HUTCHISON PORT HOLDINGS TRUST")) return true;
		 if (stockName.equals("K-GREEN TRUST")) return true;
		 if (stockName.equals("LIPPO MALLS INDO RETAIL TRUST")) return true;
		 if (stockName.equals("MAPLETREE COMMERCIAL TRUST")) return true;
		 if (stockName.equals("MAPLETREE INDUSTRIAL TRUST")) return true;
		 if (stockName.equals("MAPLETREE LOGISTICS TRUST")) return true;
		 if (stockName.equals("PERENNIAL CHINA RETAIL TRUST")) return true;
		 if (stockName.equals("PARKWAYLIFE REIT")) return true;
		 if (stockName.equals("SABANA SHARI'AH COMPLIANT REIT")) return true;
		 if (stockName.equals("SAIZEN REAL ESTATE INV TRUST")) return true;
		 if (stockName.equals("STARHILL GLOBAL REIT")) return true;
		 if (stockName.equals("SUNTEC REAL ESTATE INV TRUST")) return true;
		 return false;
	 }
}
